<?php
/**
 * Created by PhpStorm.
 * Author: DaXiong
 * Date: 2018/10/17
 * Time: 11:01
 */

namespace App\Http\Controllers\Applet;


use App\Http\Controllers\Controller;
use App\Models\AppletActivityAddress;
use Illuminate\Http\Request;

class ActivityAddressController extends Controller
{
    /**
     * 获取个人地址信息 get
     * @param Request $request
     * @param userid required
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function index(Request $request){
        $user_id = (int)$request->get('userid','');
        $info = AppletActivityAddress::where('userid',$user_id)->get();
        if(!$info->isEmpty()){
            return $this->returnJson(0,'获取个人地址信息成功',$info);
        }
        return $this->returnJson(1,'暂未提交地址信息');
    }

    /**
     * 更新个人地址信息 post
     * @param Request $request
     * @param userid required
     * @param mobile required
     * @param address required
     * @param code
     * @param name
     * @return \Illuminate\Http\JsonResponse
     * Author: DaXiong
     */
    public function update(Request $request){
        $data = $request->all();
        if(!isset($data['userid']) or !isset($data['mobile']) or !isset($data['address'])){
            return $this->returnJson(1,'缺少必要参数');
        }
        // 通过属性获取地址, 如果不存在则创建...
        $find = AppletActivityAddress::where(['userid' => $data['userid']])->first();
        if(empty($find)){
            $find = (new AppletActivityAddress);
            $find->userid = $data['userid'];
            $find->name = $data['name'];
            $find->mobile = $data['mobile'];
            $find->address = $data['address'];
            $find->code = $data['code'];
            $find->save();
            print_r($find);
        }
        if($find->update($data)){
            return $this->returnJson(0,'地址修改成功');
        };
        return $this->returnJson(1,'地址修改失败');
    }

}